package com.benyanyi.picker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.benyanyi.picker.base.BaseDialogRecyclerAdapter;

import java.util.List;

/**
 * @author YanYi
 * @date 2020/6/22 11:55
 * @email ben@yanyi.red
 * @overview
 */
class PickerTitleAdapter extends BaseDialogRecyclerAdapter<PickerTitleAdapter.TitleViewHolder> {

    private Context context;
    private List<String> list;
    private TextView tv;

    public PickerTitleAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public PickerTitleAdapter.TitleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_picker_title, parent, false);
        return new TitleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PickerTitleAdapter.TitleViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bind(context, list.get(position), position == getItemCount() - 1);
        tv = holder.getTextView();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void notifyDataSetChanged(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public TextView getTextView() {
        return tv;
    }

    static class TitleViewHolder extends RecyclerView.ViewHolder {

        private TextView titleTv;
        private View titleView;

        public TitleViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTv = itemView.findViewById(R.id.item_picker_title);
            titleView = itemView.findViewById(R.id.picker_title_view);
        }

        void bind(Context context, String title, boolean isLast) {
            titleTv.setText(title);
            if (isLast) {
                titleTv.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
                titleView.setVisibility(View.VISIBLE);
            } else {
                titleTv.setTextColor(ContextCompat.getColor(context, R.color.picker_color_black4));
                titleView.setVisibility(View.GONE);
            }
        }

        TextView getTextView() {
            return titleTv;
        }
    }
}
